package org.southyin.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.southyin.commons.CommonsResp;
import org.southyin.model.User;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

@WebServlet(value = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 处理中文乱码，数据响应类型
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");

        // 获取请求参数
        String id = req.getParameter("id");
        String[] ids = req.getParameterValues("ids");
        System.out.println(id);
        System.out.println(Arrays.toString(ids));

        // 响应请求
        // 封装数据，响应 json 数据

        User user = new User();
        user.setId(9001);
        user.setName("张三");
        CommonsResp failed = CommonsResp.failed("操作失败",user);
        String json = new ObjectMapper().writeValueAsString(failed);
        resp.getWriter().println(json);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        System.out.println(id);
    }

}
